package com.meshnetics.gb;

/**
 * Represents a pair name<->value.
 *
 * @author mmarkov
 */
public class Parameter {
    /** The name of this Parameter. */
    protected String name;

    /** The value of this Parameter. */
    protected Object val;

    /** The default value of this Parameter. */
    protected Object defaultVal;

    /**
     * Constructs the Parameter with the given name and value. The default value is set to null.
     *
     * @param name the name for the Parameter
     * @param val the value of the Parameter
     * @throws IllegalArgumentException if the given name is null
     */
    public Parameter(String name, Object val) {
        this(name, val, null);
    }

    /**
     * Constructs the Parameter with the given name, value and default value.
     *
     * @param name the name for the Parameter
     * @param val the value of the Parameter
     * @param defaultVal the default value of the Parameter
     * @throws IllegalArgumentException if the given name is null
     */
    public Parameter(String name , Object val, Object defaultVal) {
        if (name == null) {
            throw new IllegalArgumentException("Name could not be null");
        }
        this.name = name;
        this.val = val;
        this.defaultVal = defaultVal;
    }

    /**
     * Returns the name of this Parameter.
     *
     * @return the name of this Parameter
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the value of this Parameter.
     *
     * @return the value of this Parameter
     */
    public Object getValue() {
        return val;
    }

    /**
     * Sets the default value of this Parameter to the given one.
     *
     * @param obj new default value of this Parameter
     * @return old default value of this Parameter
     */
    public Object setDefaultValue(Object obj) {
        Object oldDefaultVal = defaultVal;
        defaultVal = obj;
        return oldDefaultVal;
    }

    /**
     * Returns the default value of this Parameter.
     *
     * @return the default value of this Parameter
     */
    public Object getDefaultValue() {
        return defaultVal;
    }

    /**
     * Sets the value of this Parameter to the given one.
     *
     * @param obj new value of this Parameter
     * @return old value of this Parameter
     */
    public Object setValue(Object obj) {
        Object oldVal = val;
        val = obj;
        return oldVal;
    }

    /**
     * Sets the value of this Parameter to it's default value.
     */
    public void reset() {
        val = defaultVal;
    }

    /**
     * Returns a hash code of the Parameter.
     *
     * @return a hash code for the Parameter
     */
    public int hashCode() {
        return name.hashCode();
    }

    /**
     * Compares for equality with another object.
     *
     * @param obj the Object to compare with
     * @return true if the given object is an Parameter and have the same name and value as this
     *         Parameter and false otherwise
     */
    public boolean equals(Object obj) {
        if (!(obj instanceof Parameter)) {
            return false;
        }
        Parameter p = (Parameter) obj;
        return p.name.equals(name) && ((val != null) ?  val.equals(p.val) : (p.val == null));
    }

    /**
     * Returns a string representation of this Parameter.
     *
     * @return a string representation of this Parameter
     */
    public String toString() {
        return "Parameter[name: " + name + ", val: " + val + ", default: " + defaultVal + "]";
    }
}
